<?php
require_once('includes/config.php');
ob_start();
include('includes/sc-includes.php');
$pagetitle = Contact;

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING']))
{
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

?>
<?php if ($_GET['csv']=="import") { ?>
<?php
$handle = fopen ($_FILES['csv']['tmp_name'],"r");
$first = true;
while ($data = fgetcsv ($handle, 1000, ",")) {

	//
	// The first row just has the labels
	//
	if ($first == true)
	{
		$first = false;
	}
	else
	{
		$attributes =  array(
					 "timestamp"		=> array("value" => time(), 	"replace" => "true"),
					 "contact_id"  		=> array("value" => $data['0'], "replace" => "true"),
					 "contact_first"  	=> array("value" => $data['1'], "replace" => "true"),
				 	 "contact_last" 	=> array("value" => $data['2'], "replace" => "true"),
				 	 "contact_title"  	=> array("value" => $data['3'], "replace" => "true"),
				 	 "contact_company" 	=> array("value" => $data['4'], "replace" => "true"),
				 	 "contact_street" 	=> array("value" => $data['5'], "replace" => "true"),
				 	 "contact_city"  	=> array("value" => $data['6'], "replace" => "true"),
				 	 "contact_state" 	=> array("value" => $data['7'], "replace" => "true"),
				 	 "contact_zip"		=> array("value" => $data['8'], "replace" => "true"),
				 	 "contact_email"	=> array("value" => $data['9'], "replace" => "true"),
				 	 "contact_phone"	=> array("value" => $data['10'], "replace" => "true"),
				 	 "contact_fax"	 	=> array("value" => $data['11'], "replace" => "true"),
				 	 "contact_website"	=> array("value" => $data['12'], "replace" => "true"),
				 	 "contact_profile"	=> array("value" => $data['13'], "replace" => "true"));

		$sdb->putAttributes('contacts', $data[0], $attributes);

		$history_id = uniqid();
		$history_date = time();
		$history_status = 1;

		$attributes =  array("history_contact" 	=> array("value" => $data['0'], "replace" => "true"),
							 "history_date"  	=> array("value" => time(), 	"replace" => "true"),
	 					 	 "history_status" 	=> array("value" => "1", 		"replace" => "true"));

		$sdb->putAttributes('history', $history_id, $attributes);
	}
}

header('Location: contacts.php');
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $pagetitle; ?>s</title>
<script src="includes/lib/prototype.js" type="text/javascript"></script>
<script src="includes/src/effects.js" type="text/javascript"></script>
<script src="includes/validation.js" type="text/javascript"></script>
<script src="includes/src/scriptaculous.js" type="text/javascript"></script>

<link href="includes/style.css" rel="stylesheet" type="text/css" />
<link href="includes/simplecustomer.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php include('includes/header.php'); ?>

  <div class="container">
  <div class="leftcolumn">
    <h2>Batch Import </h2>
    <table width="540" border="0" cellpadding="0" cellspacing="0">

      <tr>
        <td colspan="2">Click on &quot;Export Contacts&quot; below to see how to set up your CSV file for importing.</td>
      </tr>
      <tr>
        <td colspan="2"><form name="form1" id="form1" enctype="multipart/form-data" method="post" action="?csv=import">
            <input name="csv" type="file" id="csv" size="40" />
            <br />
            <input name="submit" type="submit" value="Import File" />
            <a href="csv.php"></a>
        </form></td>
      </tr>
      <tr>
        <td colspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2"><a href="csv.php"><strong>+ Export Contacts</strong></a></td>
      </tr>
    </table>
    <p>&nbsp; </p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </div>
  <?php include('includes/right-column.php'); ?>
  <br clear="all" />
</div>

<?php include('includes/footer.php'); ?>

</body>
</html>
